package com.ling.controller;

import com.ling.bean.Msg;
import com.ling.bean.User;
import com.ling.service.UserService;
import com.ling.util.SmsZhenZiUtil;
import com.ling.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/mine", method = RequestMethod.GET)
    public String goToHomePage() {
        return "mine";
    }

    //用户退出
    @RequestMapping(value = "/exit", method = RequestMethod.GET)
    public String exit(@Autowired HttpSession session) {
        session.setAttribute("user", null);
        return "login";
    }


    //用户跳转到找回密码页面
    @RequestMapping(value = "/findPassword", method = RequestMethod.GET)
    public String goToFindPassword() {
        return "user-findPassword";
    }

    //用户提交新的密码
    @RequestMapping(value = "/findPassword", method = RequestMethod.POST)
    @ResponseBody
    public Msg findPassword(@RequestParam String tel, @RequestParam String password, @RequestParam String code, @Autowired HttpSession session) {
        //判断用户提交的手机验证码是否正确
        String correct_code = (String) session.getAttribute("code");
        if (code.equals(correct_code)) {
            //手机验证码正确，保存新密码
            userService.changePasswordByTel(tel, password);
            return Msg.success();
        } else {
            //手机验证码错误，请重试
            return Msg.fail().add("error", "您的验证码错误，请重试！！");
        }
    }

    //用户输入手机号，判断该手机号是否存在，如果存在，则可以找回，如果不存在，则直接提示用户手机号输入错误
    @RequestMapping(value = "/existTel", method = RequestMethod.POST)
    @ResponseBody
    public Msg existTel(@RequestParam String tel) {
        //判断该手机号是否存在
        if (userService.existTel(tel)) {
            //存在该手机号，可以找回用户密码
            return Msg.success();
        } else {
            //不存在该手机号，提示用户跳转到注册页面
            return Msg.fail().add("error", "该手机号还没有注册，请先注册！！");
        }
    }


    //发送验证码
    @RequestMapping(value = "/sendCode-findPassword", method = RequestMethod.POST)
    @ResponseBody
    public Msg sendCode(@RequestParam String tel, HttpSession session) {
        //生成随机验证码,并放入session中
        String code = StringUtil.getRandomString(6);
        System.out.println("手机号："+tel+" 短信验证码："+code);
        session.setAttribute("code", code);
        if (SmsZhenZiUtil.sendSMS(tel,code)) {
            //请求成功
            return Msg.success();
        } else {
            //请求失败
            return Msg.fail().add("error","系统繁忙，请稍后重试！！！");
        }
    }

    //保存用户头像
    @RequestMapping(value = "/saveHeadImg", method = RequestMethod.POST)
    @ResponseBody
    public Msg saveHeadImg(@RequestParam String headImgUrl, @Autowired HttpSession session) {
        //判断用户是否登录
        User user = (User) session.getAttribute("user");
        if (user == null) {
            //跳转到登陆页面
            return Msg.fail().add("error", "session失效，请重新登录");
        }
        //更新session
        user.setHeadimgurl(headImgUrl);
        //保存到数据库中
        userService.saveHeadImg(headImgUrl, user.getId());
        return Msg.success();
    }

    //用户修改个人信息
    @RequestMapping(value = "/updateProfile", method = RequestMethod.POST)
    @ResponseBody
    public Msg updateProfile(@Autowired User user, @Autowired HttpSession session) {
        //判断用户id是否存在
        if (user.getId() == null) {
            return Msg.fail().add("error", "session失效，请重新登录！！");
        }

        //判断用户是否登录
        User user_session = (User) session.getAttribute("user");
        if (user_session == null) {
            return Msg.fail().add("error", "session失效，请重新登录！！");
        }

        //判断用户是否合法访问
        if (!user.getId().equals(user_session.getId())) {
            return Msg.fail().add("error", "session失效，请重新登录！！");
        }
        //保存数据库
        userService.updateUserProfile(user);
        //修改session中的数据
        session.setAttribute("user", userService.getUserById(user.getId()));
        return Msg.success();
    }

    @GetMapping("/user/{userId}")
    public String viewUser(@PathVariable String userId,HttpSession httpSession, ModelMap modelMap) {
        //当前访客
        User user = (User) httpSession.getAttribute("user");
        //要访问的用户主页信息
        User userById = userService.getUserById(userId);
        //判断是否是本人
        if ((user != null)&&(user.getId().equalsIgnoreCase(userId))) {
            //是本人，跳转到
            return "mine";
        }
        //不是本人或者未登录
        modelMap.addAttribute("userView", userById);
        return "user-view";
    }
}
